package com.office.app.admin;

/**
 * Created by jyyco on 2015/7/17.
 */
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.office.app.bussiness.action.MobileUserController;
import com.office.app.system.action.OrgController;
import com.office.app.system.model.*;
import com.office.framework.common.ExceptionAndLogInterceptor;
import com.office.framework.common.ScriptsPlugin;
import com.office.framework.flow.model.Approval;
import com.office.framework.system.action.SysIndexController;
import org.snaker.jfinal.plugin.SnakerPlugin;
import com.office.app.system.action.UserController;

public class StartConfig  extends JFinalConfig  {
    /**
     * 配置常量
     */
    public void configConstant(Constants me) {
        loadPropertyFile("jfinal.properties");
        me.setDevMode(getPropertyToBoolean("devMode", false));
//	    me.setError404View("/com.office.framework.common/404.jsp");
//	    me.setError500View("/com.office.framework.common/500.jsp");
        me.setEncoding("utf-8");
        me.setViewType(ViewType.OTHER);
        me.setBaseViewPath("WEB-INF/admin/app");
    }
    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
//        me.add("/", IndexController.class, "/system");
        me.add("/", SysIndexController.class, "/system");
        me.add("/security/user", UserController.class, "/security");
        //配置路由，设置/security/org 访问到OrgController /security 为跳转的文件夹
        me.add("/security/org", OrgController.class, "/security");
        me.add("/mobile/user", MobileUserController.class, "/mobile");
    }

    /**
     * 配置插件
     */
    public void configPlugin(Plugins me) {
        // 配置C3p0数据库连接池插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
        me.add(c3p0Plugin);

        ScriptsPlugin scriptsPlugin = new ScriptsPlugin(c3p0Plugin);
        me.add(scriptsPlugin);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        arp.addMapping("sec_user", User.class);
        arp.addMapping("sec_org", Org.class);
        arp.addMapping("sec_role", Role.class);
        arp.addMapping("sec_authority", Authority.class);
        arp.addMapping("sec_resource", Resource.class);
//        arp.addMapping("sec_menu", Menu.class);

//        arp.addMapping("conf_dictionary", Dict.class);
//        arp.addMapping("conf_dictitem", DictItem.class);
//
//        arp.addMapping("df_form", Form.class);
//        arp.addMapping("df_field", Field.class);

        arp.addMapping("flow_approval", Approval.class);
//        arp.addMapping("flow_borrow", Borrow.class);

        // 配置Snaker插件
        SnakerPlugin snakerPlugin = new SnakerPlugin(c3p0Plugin, this.prop.getProperties());
        me.add(snakerPlugin);

        // 配置shiro插件
        //暂时关闭shiro插件
//        ShiroPlugin shiroPlugin = new ShiroPlugin();
//        me.add(shiroPlugin);
    }
    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
        me.addGlobalServiceInterceptor(new ExceptionAndLogInterceptor());
    }
    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("BASE_PATH"));
    }

    public static void main(String[] args) {
        JFinal.start("web", 8888, "/", 5);
    }
}
